🧠

Virtual Memory

A memory management technique that provides the illusion of larger memory space than physically available

Introduction to Virtual Memory

Virtual memory is a memory management technique used by operating systems to provide the illusion of a larger and contiguous memory space than physically available in the main memory (RAM). It allows programs to operate as if they have access to a large, continuous block of memory, even though physical memory may be limited or fragmented.

📱

Application

Sees a large, contiguous virtual address space

🧠

Virtual Memory

Maps virtual addresses to physical addresses

💾

Physical Memory

Actual RAM installed in the system

Virtual memory creates the illusion of more memory than physically exists

Key Concepts and Functionality

Virtual memory relies on several key concepts to function effectively:

📍

Address Space

Extends the address space visible to a program beyond actual physical memory

📄

Demand Paging

Loads portions of a program only when actively needed

⚠️

Page Faults

Occurs when accessed memory is not in main memory

📋

Page Tables

Maps virtual addresses to physical addresses

🔒

Memory Protection

Isolates memory space of different processes

Address Space

Virtual memory extends the address space visible to a program beyond the actual physical memory installed on the computer. Each program sees a virtual address space that starts from zero and goes up to the maximum addressable limit, defined by the architecture and operating system.

🔍

Virtual Address Space

The range of virtual addresses that a process can use. Typically much larger than physical memory.

🔄

Address Translation

The process of converting virtual addresses to physical addresses using page tables.

📏

Contiguous Appearance

Programs see a contiguous block of memory even if physical memory is fragmented.

📍

Demand Paging

Virtual memory uses demand paging, a strategy where portions of a program's code and data are loaded into the main memory only when they are actively needed. This minimizes the amount of physical memory required to run programs and optimizes the usage of available resources.

Program Execution

Program attempts to access memory

Page Check

System checks if required page is in memory

Page Load (if needed)

If not in memory, load from disk

Execution Continues

Program resumes with required data

📄

Page Faults

When a program accesses a portion of memory that is not currently in the main memory but resides in the virtual memory, a page fault occurs. The operating system then retrieves the required data from the secondary storage (usually the hard disk) into the main memory and updates the page tables to reflect this mapping.

⚠️

Detection

Hardware detects that a required page is not in physical memory

🔄

OS Intervention

Operating system takes control to handle the page fault

💾

Page Retrieval

Required page is loaded from secondary storage

📋

Table Update

Page tables are updated to reflect the new mapping

⚠️

Page Tables

Virtual memory relies on page tables to manage the mapping between virtual addresses used by programs and physical addresses in the main memory. These tables store information about which pages of memory are currently resident in physical memory and facilitate quick lookups during address translation.

📋

Structure

Page tables are data structures that map virtual page numbers to physical frame numbers

🔍

Lookup Process

During address translation, the CPU uses the page table to find the physical location

📊

Multi-level Tables

Large systems use hierarchical page tables to reduce memory overhead

📋

Memory Protection

Virtual memory systems provide memory protection mechanisms to isolate and protect the memory space of different processes from unauthorized access. This ensures that each program operates within its designated memory boundaries, enhancing system security and stability.

🔒

Access Control

Page tables include permission bits that control read, write, and execute access

🚫

Isolation

Prevents one process from accessing another's memory without authorization

🛡️

Security

Protects system memory and critical data from malicious or accidental modification

🔒

Benefits of Virtual Memory

Efficient Memory Management

Allows efficient utilization of physical memory by dynamically swapping data between main memory and secondary storage as needed, optimizing overall system performance.

📈

Support for Large Applications

Enables the execution of large applications that require more memory than available physical RAM, enhancing the capabilities of modern software systems.

💻

Simplified Programming

Programmers can write code without worrying about physical memory limitations, as the operating system manages memory allocation and paging transparently.

Implementation and Considerations

Virtual memory is implemented by the operating system using a combination of hardware support (such as memory management units in CPUs) and software algorithms (like page replacement policies). Efficient management of virtual memory requires balancing factors such as page size, page replacement algorithms (e.g., LRU - Least Recently Used), and disk I/O performance to minimize overhead and maximize system responsiveness.

🔧

Hardware Support

Memory Management Units (MMUs) in CPUs handle address translation and page fault detection

🔄

Page Replacement

Algorithms like LRU, FIFO, and Clock determine which pages to evict when memory is full

💿

Disk I/O Optimization

Efficient disk access patterns and caching strategies to minimize page load times

⚙️

Paging

Paging divides physical memory into fixed-size blocks called pages and logical memory into blocks of the same size called frames. Pages are the unit of data transfer between secondary storage and main memory.

📋

Page Table

Each process has a page table that maps virtual pages to physical frames. The page table typically resides in main memory and is managed by the operating system. It translates virtual addresses generated by the CPU into physical addresses.

⚠️

Page Fault

When a program references a page not currently in main memory (a page fault occurs), the operating system loads the required page from secondary storage (e.g., disk) into a free frame in main memory. This process is known as demand paging.

🔄

Page Replacement

If all frames are occupied and a page fault occurs, the operating system must replace a page in main memory with the required page. Various algorithms like Least Recently Used (LRU), First-In-First-Out (FIFO), and Clock are used for page replacement decisions.

Benefits

Paging allows efficient use of physical memory by allocating memory on-demand, supports memory protection through page-level permissions, and simplifies memory allocation by using fixed-size pages.

📄

Segmentation

Segmentation divides a program's address space into variable-sized logical segments (such as code, data, stack) rather than fixed-size pages.

📋

Segment Table

Each segment is mapped to a segment table entry that stores the base address and size of the segment in main memory. The segment table is typically stored in the CPU and is indexed by a segment number obtained from the virtual address.

🔄

Address Translation

When a virtual address is generated by the CPU, the segment number is used to index the segment table to retrieve the base address of the segment. The offset within the segment is then added to the base address to obtain the physical address.

⚠️

Segmentation Fault

Similar to page faults, segmentation faults occur when a program attempts to access a segment that is not present in main memory or violates memory protection rules.

Benefits

Segmentation allows for more flexible memory allocation than paging, as segments can vary in size and type (code, data, stack). It supports modular program design and simplifies memory management by providing a hierarchical view of memory.

📦

Comparison: Paging vs Segmentation

Hierarchical view with logical segments

Aspect Paging Segmentation
📏 Granularity Uses fixed-size pages Uses variable-sized segments
🔄 Address Translation Translates virtual addresses to physical addresses using page tables Uses segment tables to map segments to physical memory
🔧 Flexibility Less flexible due to fixed page sizes More flexible memory allocation with variable segment sizes
🗂️ Fragmentation Can cause internal fragmentation Can cause external fragmentation
📊 Memory View Linear view of memory
⚖️

Combined Approach: Paging and Segmentation

Modern virtual memory systems often combine paging and segmentation techniques to leverage their respective advantages. This hybrid approach, known as paged segmentation or segmented paging, allows for both flexible memory allocation and efficient use of physical memory.

🔄

Segmented Paging

First divides memory into logical segments, then each segment is divided into fixed-size pages

📊

Two-Level Translation

Virtual address is translated using both segment table and page table

Benefits

Combines logical organization of segmentation with efficient memory management of paging

🔀

This combined approach provides the best of both worlds: the logical organization and protection benefits of segmentation, along with the efficient memory utilization and simplified allocation benefits of paging. It is widely used in modern operating systems like Windows, Linux, and macOS to provide robust and efficient memory management.